package com.hidt.shop.controller;

import com.fasterxml.jackson.core.io.JsonEOFException;
import com.hidt.shop.domain.Advertisement;
import com.hidt.shop.domain.AdvertisementGoods;
import com.hidt.shop.service.AdvertisementService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class AdvertisementController {

    @Autowired
    private AdvertisementService advertisementService;

    /**
     *@methodName addAdvertisement
     *@方法功能描述
     * 增加轮播图
     * big_pic,goods_id
     *@params
     * @param map
     *@return int
     *@exception
     *@author $風~$
     *@Date 2021/1/21
     */
    @RequestMapping(value = "/addAdvertisement", method = {RequestMethod.POST})
    public  String  addAdvertisement(Map map , MultipartFile img , String goods_id ,HttpServletRequest req) throws IOException {
        System.out.println(map);
        System.out.println("dffdsgvsrdfg");
        // 指定文件上传后的物理位置
        String path = "F:\\培训笔记\\第二次项目\\img";
        // 获取文件名
        String filename = img.getOriginalFilename();
        //新的名字
        String new_file_name = UUID.randomUUID() +"-"+ filename;
        map.put("big_pic",new_file_name);
        map.put("goods_id",goods_id);
        File f = new File(path, new_file_name);
        if (!f.exists()) {
            FileUtils.copyInputStreamToFile(img.getInputStream(), new File(req.getServletContext().getRealPath("/images/") + new_file_name));
            // 上传文件
            img.transferTo(f);

        }
        int i = advertisementService.addAdvertisement(map);
        return new_file_name;
    }

    /**
     *@methodName deleteAdvertisement
     *@方法功能描述
     * （伪）删除轮播图
     *@params
     * @param map
     *@return int
     *@exception
     *@author $風~$
     *@Date 2021/1/21
     */
    @RequestMapping(value = "/deleteAdvertisement",method = RequestMethod.POST)
    public  int deleteAdvertisement(@RequestBody  Map map) throws JsonEOFException {
        int i = advertisementService.deleteAdvertisement(map);
        return i;
    }

    /**
     *@methodName updateAdvertisement00
     *@方法功能描述
     * 修改轮播图片
     *@params
     * @param
     *@return int
     *@exception
     *@author $風~$
     *@Date 2021/1/21
     */
    @RequestMapping(value = "/updateAdvertisement",method = RequestMethod.POST)
    public  int updateAdvertisement(Advertisement advertisement, MultipartFile img , HttpServletRequest req) throws IOException {
        System.out.println("修改轮播图");
        // 指定文件上传后的物理位置
        String wpath = "C:\\Users\\DELL\\Desktop\\img";
        // 指定文件上传后的服务器地址
        String fpath = "";
        // 获取文件名
        System.out.println(img);
        System.out.println(advertisement);
//        System.out.println(goods_id);
        System.out.println(req);
        String filename = img.getOriginalFilename();
        Map map = new HashMap();
        //新的名字
        String new_file_name = UUID.randomUUID() +"-"+ filename;
        map.put("big_pic",new_file_name);
        map.put("advertisement_id",advertisement.getAdvertisement_id());
//        map.put("goods_id",goods_id);
//        map.put("advertisement_id",advertisement_id);
        System.out.println(map.get("big_pic"));
        File f = new File(wpath, new_file_name);
        if (!f.exists()) {
            // 上传文件
            FileUtils.copyInputStreamToFile(img.getInputStream(), new File(req.getServletContext().getRealPath("/images/") + new_file_name));
            img.transferTo(f);

            System.out.println("dddddddddddddddddddddddddddddddddddddddd");
            System.out.println(req.getServletContext().getRealPath("/images/") );
        }
        int i = advertisementService.updateAdvertisement(map);
        return i;
    }

    /**
     *@methodName selectAdvertisement
     *@方法功能描述
     * 查询轮播图
     *@params
     * @param map
     *@return java.util.List<com.hidt.shop.domain.Advertisement>
     *@exception
     *@author $風~$
     *@Date 2021/1/21
     */
    @RequestMapping("/selectAdvertisement")
    public List<AdvertisementGoods> selectAdvertisement(@RequestBody  Map map) throws JsonEOFException {
        List<AdvertisementGoods> advertisements = advertisementService.selectAdvertisement(map);
        return advertisements;
    }
}
